/*
    COMP9517 Computer Vision, Semester 2, 2011 - University of New South Wales
   ============================================================================
   |   Group .  | Christian Mostegel & Dong Back Kim                          |
   ============================================================================
 */

#ifndef IMAGEANALYZER_H_
#define IMAGEANALYZER_H_

#include "opencv2/opencv.hpp"
#include "AnalyzedImage.h"
class ImageAnalyzer
{
public:

    static ImageAnalyzer & getInstance()
    {
        static ImageAnalyzer instance;
        return instance;
    }

    void detectKeyPoints( const cv::Mat& image, std::vector<cv::KeyPoint>& keypoints );
    void computeDescriptors( const cv::Mat& image, std::vector<cv::KeyPoint>& keypoints, cv::Mat& descriptors );
    void match(const AnalyzedImage & img1, const AnalyzedImage & img2, std::vector<cv::DMatch>& matches);
    long int  getTotalDetectionTime(){return total_detection_time_;}
    long int  getTotelDescTime(){return total_desc_time_;}
    unsigned long getTotalNumFeatures(){return total_num_features_;}
    long int  getTotalMatchTime(){return total_match_time_;}

private:

    void getSingleMatches(std::vector<std::vector<cv::DMatch> > & matches1to2 ,std::vector<cv::DMatch> & single_matches1to2);
    void getSingleReversedMatches(std::vector<std::vector<cv::DMatch> > & matches1to2 ,std::vector<cv::DMatch> & single_matches1to2);

    void crossCheckMatch( const cv::Mat& desc1, const cv::Mat& desc2,
                          std::vector<cv::DMatch>& common_matches, float maxDistance);
    void kdTreeMatch( const AnalyzedImage & img1, const AnalyzedImage & img2, std::vector<cv::DMatch>& matches, float maxDistance);

    ImageAnalyzer();
    virtual ~ImageAnalyzer();
    cv::DescriptorExtractor *  desc_extractor_;
    cv::Ptr<cv::FeatureDetector> fd_;
    cv::BruteForceMatcher<cv::L2<float> > desc_matcher_;
    float max_distance_ ;
    long int  total_detection_time_;
    long int total_desc_time_;
    unsigned long total_num_features_;
    long int total_match_time_;
};

#endif /* IMAGEANALYZER_H_ */
